package com.beaven.java.search;

import java.util.Arrays;

/**
 * @author Beaven
 * @date 2018/7/12 21:33
 */
public class BinarySearch {

    public static int search(int[] nums, int value) {
        Arrays.sort(nums);
        int lo = 0;
        int hi = nums.length - 1;
        while (lo <= hi) {
            int mid = lo + (hi - lo) / 2;
            if (value < nums[mid]) {
                hi = mid - 1;
            } else if (value > nums[mid]) {
                lo = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
}
